﻿Imports System.Text
Imports System.Runtime.InteropServices

Module APIs
    Public Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
    Public Declare Auto Sub SetForegroundWindow Lib "user32.dll" (ByVal hWnd As IntPtr)

    Public Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As IntPtr, ByRef lpwndpl As WINDOWPLACEMENT) As Integer

    Public Structure POINTAPI
        Public x As Integer
        Public y As Integer
    End Structure

    Public Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure

    Public Structure WINDOWPLACEMENT
        Public Length As Integer
        Public flags As Integer
        Public showCmd As Integer
        Public ptMinPosition As POINTAPI
        Public ptMaxPosition As POINTAPI
        Public rcNormalPosition As RECT
    End Structure

    Public Declare Auto Function GetLastActivePopup Lib "user32.dll" (ByVal hwnd As IntPtr) As IntPtr
    Public Declare Function GetAncestor Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal gaFlags As UInteger) As IntPtr
    Public Declare Function GetShellWindow Lib "user32.dll" () As IntPtr

    Public Declare Auto Function GetClassName Lib "user32" (ByVal hWnd As IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer
    Public Declare Function IsWindowVisible Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean

    Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Int32, ByVal lpString As StringBuilder, ByVal cch As Int32) As Int32

    Dim Windows As New List(Of IntPtr)

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As UInteger) As Boolean
    End Function

    Public Delegate Function CallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean

    Declare Function EnumWindows Lib "user32" (ByVal x As CallBack, ByVal y As Integer) As Integer

    Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Int32) As Int32
    Public Declare Function GetParent Lib "user32.dll" (ByVal intptr As IntPtr) As IntPtr
    Public Const GWL_HWNDPARENT As Int32 = -8
    Public Const GWL_EXSTYLE = (-20)
    Public Const WS_EX_APPWINDOW = &H40000
    Public Const WS_OVERLAPPED = &H0L

    Public Function GetWindows() As List(Of IntPtr)
        Windows = New List(Of IntPtr)
        EnumWindows(AddressOf Report, 0)
        Return Windows
    End Function

    Private Function Report(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
        Windows.Add(hwnd)
        Return True
    End Function

    Public Function IsWindowMinimized(ByVal hwnd As IntPtr) As Boolean
        Dim intRet As Integer
        Dim wpTemp As WINDOWPLACEMENT

        wpTemp.Length = System.Runtime.InteropServices.Marshal.SizeOf(wpTemp)
        intRet = GetWindowPlacement(hwnd, wpTemp)


        If wpTemp.showCmd = 2 Then
            Return True
        End If
        Return False
    End Function
End Module
